import 'dart:io';

//更新文件名----更新文件内容
void main() {
  // 当前脚本所在目录
  final scriptDir = File(Platform.script.toFilePath()).parent;
  final dirName = scriptDir.uri.pathSegments.lastWhere((e) => e.isNotEmpty);

  print('当前目录: ${scriptDir.path}');
  print('目录名: $dirName');
  // 遍历目录下的文件
  for (var entity in scriptDir.listSync()) {
    if (entity is File && entity.path.endsWith('.dart')) {
      final fileName = entity.uri.pathSegments.last;
      // 跳过脚本自身
      if (fileName == 'rename.dart') continue;
      var oldFileNanme = entity.path.split('\\').last;
      var suffix = oldFileNanme.split('_').last;
      final newName = '${dirName}_$suffix';
      final newPath = '${scriptDir.path}/$newName';
      final content = entity.readAsStringSync();
      //替换import 部分
      final replaced1 = content.replaceAll('import \'template', 'import \'$dirName');
      //替换占位名称部分
      final replaced2 = replaced1.replaceAll('template', toCamelCase(dirName));
      // 覆盖写回文件
      entity.writeAsStringSync(replaced2);
      entity.renameSync(newPath);
      print('重命名: $fileName → $newName');
    }
  }
}

String toCamelCase(String input) {
  // 全小写 + 按 "_" 分割
  final parts = input.toLowerCase().split('_');
  return parts.skip(0).map((word) {
    if (word.isEmpty) return '';
    return word[0].toUpperCase() + word.substring(1);
  }).join();
}
